package data

import (
	"prombridge/internal/biz"
	"sync"
)

type Memstore struct {
	instances sync.Map
}

func NewMemstore() biz.Store {
	return &Memstore{instances: sync.Map{}}
}

func (Memstore) Name() string {
	return "memstore"
}

func (m *Memstore) Get() []biz.PromJson {
	var (
		s = make([]biz.PromJson, 0)
	)
	m.instances.Range(func(key, value interface{}) bool {
		s = append(s, biz.PromJson{
			Targets: []string{key.(string)},
			Labels:  nil,
		})
		return true
	})
	return s
}

func (m *Memstore) Add(instance []string) error {
	for _, i := range instance {
		m.instances.Store(i, struct {
		}{})
	}
	return nil
}

func (m *Memstore) Clean() {
	m.instances = sync.Map{}
}
